﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dreametryVersion2
{
    public partial class frmAddEmployee : Form
    {
        DataTable data;
        
        public frmAddEmployee()
        {
            InitializeComponent();
        }

        private void frmAdmin_Load(object sender, EventArgs e)
        {


            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                //sql command and fill data to datatable
                SqlCommand command = new SqlCommand("SELECT Designer, Verifier, Uploader, AdminUser FROM RoleTable", connection);
                command.Connection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                data = new DataTable();
                data.Clear();
                adapter.Fill(data);

            }



            foreach (DataColumn x in data.Columns)
            {
                checkedList.Items.Add(x.ColumnName);
            }

            int i;
            string y;
            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                SqlCommand command = new SqlCommand("SELECT TOP 1 emp_id FROM employee ORDER BY emp_id DESC;", connection);
                command.Connection.Open();
                y = command.ExecuteScalar().ToString();
            }
            i = Convert.ToInt32(y)+1;
            txtid.Text = i.ToString();

        }

        private void btnReturn_Home_Click(object sender, EventArgs e)
        {
            frmMain fra = new frmMain();
            fra.Show();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                //sql command and fill data to datatable
                SqlCommand command = new SqlCommand("INSERT INTO employee(emp_id, fullname, folder, username, password, picture) VALUES(@id, @fullname, @folder, @username, @password, @picture)", connection);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@id", Convert.ToInt32(txtid.Text));
                command.Parameters.AddWithValue("@fullname", txtFullname.Text);
                command.Parameters.AddWithValue("@folder", lblTaskinprogress.Text);
                command.Parameters.AddWithValue("@username", txtUsername.Text);
                command.Parameters.AddWithValue("@password", global.CalculateMD5Hash(txtPassword.Text));
                MemoryStream stream = new MemoryStream();
                picEmployee.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] picture = stream.ToArray();
                command.Parameters.AddWithValue("@picture", picture);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                //sql command and fill data to datatable
                SqlCommand command = new SqlCommand("INSERT INTO RoleTable(emp_id) VALUES(@id)", connection);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@id", txtid.Text);
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            for (int i = 0; i < checkedList.CheckedItems.Count; i++)
            {
                string name = checkedList.CheckedItems[i].ToString();
                using (SqlConnection connection = new SqlConnection(global.conString))
                {
                    //sql command and fill data to datatable
                    SqlCommand command = new SqlCommand("UPDATE RoleTable SET "+name+" = 'y' WHERE emp_id = @id", connection);
                    command.CommandType = CommandType.Text;
                    command.Parameters.AddWithValue("@id", txtid.Text);
                    command.Parameters.AddWithValue("@item", checkedList.CheckedItems[i].ToString());
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                }

            }

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "Select Task in Progress personal foder";
            folderBrowserDialog1.SelectedPath = @"Z:\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS";
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = Path.GetFullPath(folderBrowserDialog1.SelectedPath);
                string lastDirectory = path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();
                lblTaskinprogress.Text = lastDirectory;
            }
        }

        private void picEmployee_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "jpg|*.jpg";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                picEmployee.Image = Image.FromFile(openFileDialog1.FileName);
            }
        }
    }
}
